<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>
        后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-button @click="loadAlbumList" style="margin: 0 0 10px 0">
      <i class="el-icon-refresh-left"></i>
      刷新
    </el-button>

    <template>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="name" label="相册名称" width="230" header-align="center"></el-table-column>
        <el-table-column prop="description" label="相册简介" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 弹出的编辑相册的对话框   -->
    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="相册名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相册简介" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditDialog()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        description: '',
        sort: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitEditDialog() {
      let url = 'http://localhost:9080/albums/'+this.form.id+'/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.form);
      console.log("formData = " + formData);

      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.dialogFormVisible = false;
          this.loadAlbumList();
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          })
        }else if (responseBody.state == 40400){
          this.dialogFormVisible = false;
          this.$message.error(responseBody.message);
          this.loadAlbumList();
        }else if (responseBody.state == 40900){
          this.$message({
            message: responseBody.message,
            type: 'warning'
          });
        }
      })

    },
    openEditDialog(album) {
      // this.dialogFormVisible = true;
      let url = 'http://localhost:9080/albums/' + album.id;
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.form = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(responseBody.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          })
        }
      })
    },
    openDeleteConfirm(album) {
      this.$confirm('此操作将永久删除【' + album.name + '】相册, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);
      }).catch(() => {

      });
    },
    handleDelete(album) {
      let url = 'http://localhost:9080/albums/'+album.id+'/deleteById';
      console.log('url = ' + url);
      let formData = 'id=' + album.id;
      console.log('formData = ' + formData)
      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.loadAlbumList();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        } else {
          // this.$message.error(responseBody.message);
          this.$alert(responseBody.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }
      })
    },
    loadAlbumList() {
      let url = 'http://localhost:9080/albums'
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    }
  },
  mounted() {
    this.loadAlbumList();
  }
}
</script>

<style scoped>

</style>